package cn.edu.sdjzu.xg.kcsj.controller;

import cn.edu.sdjzu.xg.kcsj.domain.Classroom;
import cn.edu.sdjzu.xg.kcsj.domain.Teacher;
import cn.edu.sdjzu.xg.kcsj.service.ClassroomService;
import cn.edu.sdjzu.xg.kcsj.service.TeacherService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import util.Condition;
import util.ControllerHelper;
import util.JSONUtil;
import util.Pagination;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/selectTeacher.ctl")
public class SelectTeacherController extends HttpServlet {
    @Override
    protected void doGet (HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        JSONObject respMessage = new JSONObject ();
        resp.setContentType ("application/json;charset=UTF-8");
        String id_str = req.getParameter ("id");
        if (id_str == null) {
            respMessage.put ("message","no have request");
            resp.getWriter ().println (respMessage);
        } else {
            int id_int = Integer.parseInt (id_str);
            try {
                responseTeacher (id_int,resp);
            } catch (SQLException throwables) {
                throwables.printStackTrace ();
            }
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject respMessage_jsonObj = new JSONObject();
        String req_jsonStr = JSONUtil.getJSON(req);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj == null) {
            respMessage_jsonObj.put("message", "no have request");
            resp.getWriter().println(respMessage_jsonObj);
        }
        try {
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            this.responseTeacher(pagination, conditionList, resp);
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "sjk have exception");
        } catch (ServletException e) {
            respMessage_jsonObj.put("message", "ServletException");
        } catch (IOException e) {
            respMessage_jsonObj.put("message", "IOException");
        }
    }

    private void responseTeacher(Pagination pagination, List<Condition> conditionList, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        JSONObject message = new JSONObject();
        Collection<Classroom> classrooms = ClassroomService.getInstance().findAll(pagination, conditionList);
        message.put("totalNum", pagination.getTotalNum());
        message.put("totalPages", pagination.getTotalPages(pagination));
        message.put("data", classrooms);
        response.getWriter().println(message);
    }

    private void responseTeacher (int id_int,HttpServletResponse resp) throws IOException, SQLException {
        Teacher teacher = TeacherService.getInstance().find(id_int);
        resp.getWriter().println(teacher);
    }
}